package cz.tomasdvorak.cars.pages;

import cz.tomasdvorak.cars.CarExpensesApp;
import cz.tomasdvorak.cars.dao.ExpensesDao;
import cz.tomasdvorak.cars.dao.UsersDao;
import cz.tomasdvorak.cars.dao.VehiclesDao;
import cz.tomasdvorak.cars.dto.Role;
import cz.tomasdvorak.cars.pages.expenses.AllExpensesPage;
import cz.tomasdvorak.cars.pages.expenses.ExpenseEditingPage;
import cz.tomasdvorak.cars.pages.expenses.StatsPage;
import cz.tomasdvorak.cars.pages.signing.UtilitiesPanel;
import cz.tomasdvorak.cars.pages.user.AllUsersPage;
import cz.tomasdvorak.cars.pages.user.RegisterUserPage;
import cz.tomasdvorak.cars.pages.vehicle.AllVehiclesPage;
import cz.tomasdvorak.cars.pages.vehicle.VehicleEditingPage;
import cz.tomasdvorak.cars.utils.Lists;
import cz.tomasdvorak.cars.wicket.components.auth.MetadataAuthorizationStrategy;
import cz.tomasdvorak.cars.wicket.components.breadcrumbs.BreadcrumbItem;
import cz.tomasdvorak.cars.wicket.components.breadcrumbs.BreadcrumbPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.ResourceModel;

import java.util.ArrayList;
import java.util.List;

public abstract class BasePage extends WebPage {

    protected void addBreadcrumb() {
        if (get("breadcrumb") == null) {
            add(new BreadcrumbPanel("breadcrumb", getBreadcrumb()));
        }

        super.onBeforeRender();
    }

     protected List<BreadcrumbItem> getBreadcrumb() {
        ArrayList<BreadcrumbItem> breadcrumbs = Lists.arrayList(new BreadcrumbItem(new ResourceModel("breadcrumb.home"),
                MainPage.class));
        try {
            breadcrumbs.addAll(getPageBreadcrumb());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return breadcrumbs;
    }

    protected abstract List<BreadcrumbItem> getPageBreadcrumb() throws Exception;

    public BasePage() {
        addBreadcrumb();
        add(new BookmarkablePageLink<AllVehiclesPage>("allVehiclesPage", AllVehiclesPage.class));
        add(new BookmarkablePageLink<VehicleEditingPage>("addNewVehiclePage", VehicleEditingPage.class));

        add(new BookmarkablePageLink<HelpPage>("helpPage", HelpPage.class));

        add(new BookmarkablePageLink<AllExpensesPage>("allExpensesPage", AllExpensesPage.class));
        add(new BookmarkablePageLink<ExpenseEditingPage>("addNewExpense", ExpenseEditingPage.class));
        add(new BookmarkablePageLink<ExpenseEditingPage>("stats", StatsPage.class));

        add(new BookmarkablePageLink<RegisterUserPage>("registerUserPage", RegisterUserPage.class));
        BookmarkablePageLink<AllUsersPage> allUsersLink = new BookmarkablePageLink<AllUsersPage>(
                "allUsersPage", AllUsersPage.class);
        MetadataAuthorizationStrategy.authorize(allUsersLink, Role.ADMIN.name());
        add(allUsersLink);



        add(new UtilitiesPanel("loginPanel"));
    }

    protected UsersDao getUserDao() {
        return ((CarExpensesApp) getApplication()).getUsersDao();
    }

    protected VehiclesDao getVehiclesDao() {
        return ((CarExpensesApp) getApplication()).getVehiclesDao();
    }

    protected ExpensesDao getExpensesDao() {
        return ((CarExpensesApp)getApplication()).getExpensesDao();
    }
}
